/**
 * jims
 */
package com.jims.api.v1.controller.doctor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jims.master.common.data.Message;
import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.sync.entity.*;
import com.jims.master.modules.sync.service.*;
import com.jims.master.modules.sys.entity.Dict;
import com.jims.master.modules.sys.service.DictService;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

/**
 * 检验申请Controller
 * @author jict
 * @version 2017-03-16
 */
@RestController
@RequestMapping(value = "/labApply")
public class LabApplyController extends BaseController {

	@Autowired
	private LabApplyService labApplyService;
    @Autowired
    private LabTypeService labTypeService;//检验项目Service
    @Autowired
    private ExamTypeService examTypeService;//检查项目业务
    @Autowired
    private LabItemService labItemService;
    @Autowired
    private OrgDeptService orgDeptService;
    @Autowired
    private DictService dictService;
    @Autowired
    private ClinicMasterService clinicMasterService;

    /**
     * 医生查看检验申请
     * @param clinicId
     * @Author ct
     * @return
     */
    @RequestMapping(value = {"V100/getLabApply"})
    public String getLabApply(String clinicId){
        List<LabApply> labApplyList = labApplyService.getByClinicId(clinicId);
        Message mes = new Message();
        mes.setData(labApplyList);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 删除检验
     * @param id
     * @Author ct
     * @return
     */
    @RequestMapping(value = {"V100/deleteLab"})
    public String deleteLabV100(@RequestParam(required = true) String id){
        Message mes = new Message();
        int i = labApplyService.deleteLab(id);
        mes.setData(i);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 医生查看检验申请详情
     * @param id
     * @Author ct
     * @return
     */
    @RequestMapping(value = {"V100/getLabApplyById"})
    public String getLabApplyById(String id){
        LabApply labApply = labApplyService.get(id);
        Message mes = new Message();
        mes.setData(labApply);
        return SecurityUtil.encryptMsgData(mes);
    }

      /**
     * 检验申请保存
     * @return
     * @author jict
     */
      @RequestMapping(value = {"V100/saveLab"})
	public String saveLab(String clinicId,String patientId,String orgId,String clincItemIds,String implementDept,String labType,String type,String num,String label) throws Exception{
        Dict dict = dictService.getValues(label);
        LabApply labApply = new LabApply();
        labApply.setClinicId(clinicId);
        labApply.setOrgId(orgId);
        labApply.setPatientId(patientId);
        labApply.setLabType(labType);
        labApply.setImplementDept(implementDept);
        labApply.setSpecimen(dict.getValue());
        Message mes = new Message();
		int i=labApplyService.saveLabApplyData(labApply,clincItemIds,type,num);
        if(i>0){
            mes.setData("保存检验申请成功");
        }else{
            mes.setData("保存检验申请失败");
        }
        return SecurityUtil.encryptMsgData(mes);
	}

    /**
     * 检验申请保存
     * @return
     * @author jict
     */
    @RequestMapping(value = {"V200/saveLab"})
    public String saveLabs(String clinicId,String patientId,String orgId,String clincItemIds,String implementDept,String labType,String type,String num,String label) throws Exception{
        Dict dict = dictService.getValues(label);
        LabApply labApply = new LabApply();
        labApply.setClinicId(clinicId);
        labApply.setOrgId(orgId);
        labApply.setPatientId(patientId);
        labApply.setLabType(labType);
        labApply.setImplementDept(implementDept);
        labApply.setSpecimen(dict.getValue());
        Message mes = new Message();
        int i=labApplyService.saveLabApplyDatas(labApply,clincItemIds,type,num);
        if(i>0){
            mes.setData("保存检验申请成功");
        }else{
            mes.setData("保存检验申请失败");
        }
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 获取检验大类别
     * @Author jict
     */
    @RequestMapping(value = "V100/labType", method = {RequestMethod.GET, RequestMethod.POST})
    public String labType(String orgId, HttpServletRequest request, HttpServletResponse response) {
        //1 查询检验项目
        LabType labType= new LabType();
        labType.setLevel("1");
        labType.setOrgId(orgId);
        List<LabType> labTypeList=labTypeService.findList(labType);
        Message mes = new Message();
        mes.setData(labTypeList);
        return  SecurityUtil.encryptMsgData(mes);
    }

    /**
     * 根据检验项目id查找其诊疗项目集合
     * @param labType
     * @param request
     * @param response
     * @return
     * @Author jict
     */
    @RequestMapping(value = "V100/queryByPId", method = {RequestMethod.GET, RequestMethod.POST})
    public String queryByPId(LabType labType,HttpServletRequest request, HttpServletResponse response) {
        List<LabType> list = this.labTypeService.queryBylabTypeId(labType.getId());
        Message mes = new Message();
        mes.setData(list);
        return SecurityUtil.encryptMsgData(mes);
    }


}